﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;

using System.Text;
using System.Windows.Forms;

namespace Movies
{
    public partial class FormApplyFilter : Form
    {
        public FormApplyFilter()
        {
            InitializeComponent();
        }

        private void mainButton_Click(object sender, EventArgs e)
        {
            DTOFilter filter = new DTOFilter();
            bool isErrorInProcessing = false;
            bool isFilterAplied = false;

            if (seenStatus.SelectedIndex != 0)
            {
                filter.SeenStatus = seenStatus.SelectedIndex;
                isFilterAplied = true;
            }

            if (IsTitleEnabled.Checked)
            {
                filter.Title = title.Text.Trim();
                isFilterAplied = true;
            }

            if (IsRatingHighEnabled.Checked)
            {
                try
                {
                    filter.RatingHigh = Convert.ToDouble(ratingsHigh.Text.Trim());
                    isFilterAplied = true;
                    ratingsHigh.BackColor = Color.White;
                }
                catch (Exception ex)
                {
                    ratingsHigh.BackColor = Color.Pink;
                    isErrorInProcessing = true;
                    HelperContentProvider.OmitWarning(ex);
                }
            }

            if (IsRatingLowEnabled.Checked)
            {
                try
                {
                    filter.RatingLow = Convert.ToDouble(ratingsLow.Text.Trim());
                    isFilterAplied = true;
                    ratingsLow.BackColor = Color.White;
                }
                catch (Exception ex)
                {
                    ratingsLow.BackColor = Color.Pink;
                    isFilterAplied = true;
                    isErrorInProcessing = true;
                    HelperContentProvider.OmitWarning(ex);
                }
            }

            if (IsYearEnabled.Checked)
            {
                
                try
                {
                    filter.ReleaseYear = Convert.ToInt32(releaseYear.Text.Trim());
                    isFilterAplied = true;
                    releaseYear.BackColor = Color.White;
                }
                catch (Exception ex)
                {
                    releaseYear.BackColor = Color.Pink;
                    isErrorInProcessing = true;
                    HelperContentProvider.OmitWarning(ex);
                }
            }

            if(isErrorInProcessing){
                return;
            }

            if (IsThroughEnabled.Checked)
            {
                try
                {
                    filter.TillYear = Convert.ToInt32(through.Text.Trim());
                    isFilterAplied = true;
                    through.BackColor = Color.White;
                }
                catch (Exception ex)
                {
                    through.BackColor = Color.Pink;
                    isErrorInProcessing = true;
                    HelperContentProvider.OmitWarning(ex);
                }

            }

            if (IsRateEnabled.Checked)
            {
                filter.Rate = rate.Text.Trim();
                isFilterAplied = true;
            }

            if (IsDirectorEnabled.Checked)
            {
                filter.Director = director.Text.Trim();
                isFilterAplied = true;
            }

            if (IsCastEnabled.Checked)
            {
                filter.Cast = cast.Text.Trim();
                isFilterAplied = true;
            }

            if (IsWriterEnabled.Checked)
            {
                filter.Writer = writer.Text.Trim();
                isFilterAplied = true;
            }

            List<string> genresList = new List<string>();
            foreach (Control  checkBox in genresGroup.Controls)
            {
                CheckBox genres = (CheckBox)checkBox;
                if (genres.Checked)
                {
                    genresList.Add(genres.Text.ToLower());
                    isFilterAplied = true;
                }
            }
            if (genresList.Count > 0)
            {
                filter.Genre = genresList;
                isFilterAplied = true;
            }

            this.Hide();

            if (!isFilterAplied && !HelperDataProvider.getDataStore().isFilterDefined())
            {
                clearFilter.Hide();
                return;

            }
            else if(!isFilterAplied)
            {
                clearFilter.Hide();
                filter = null;
            }        
            else
            {
                clearFilter.Show();
            }

            HelperDataProvider.getDataStore().addFilter(filter);
            WinDisplayControllor.ApplyChangesOnLoadedWin();
            
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            WinDisplayControllor.DisposeApplyFilterWin();
            if (HelperDataProvider.getDataStore().isFilterDefined())
            {
                HelperDataProvider.getDataStore().addFilter(null);
                WinDisplayControllor.ApplyChangesOnLoadedWin();
            }

        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void enableCheckIfRequired(Control textField, CheckBox option)
        {
            if (textField.Text.Trim().Length > 0)
            {
                option.Checked = true;
            }
            else
            {
                option.Checked = false;
            }
        }

        private void title_TextChanged(object sender, EventArgs e)
        {
            enableCheckIfRequired(title, IsTitleEnabled);
        }

        private void ratingsHigh_Changed(object sender, EventArgs e)
        {
            enableCheckIfRequired(ratingsHigh, IsRatingHighEnabled);
        }

        private void ratingsLow_Changed(object sender, EventArgs e)
        {
            enableCheckIfRequired(ratingsLow, IsRatingLowEnabled);
        }

        private void director_TextChanged(object sender, EventArgs e)
        {
            enableCheckIfRequired(director, IsDirectorEnabled);
        }

        private void cast_TextChanged(object sender, EventArgs e)
        {
            enableCheckIfRequired(cast, IsCastEnabled);
        }

        private void writer_TextChanged(object sender, EventArgs e)
        {
            enableCheckIfRequired(writer, IsWriterEnabled);
        }

        private void rate_Changed(object sender, EventArgs e)
        {
            enableCheckIfRequired(rate, IsRateEnabled);
        }

        private void releaseYear_TextChanged(object sender, EventArgs e)
        {
            enableCheckIfRequired(releaseYear, IsYearEnabled);
        }

        private void through_TextChanged(object sender, EventArgs e)
        {
            enableCheckIfRequired(through, IsThroughEnabled);
        }

        private void releaseYear_KeyDown(object sender, KeyEventArgs e)
        {

        }

        private void through_KeyPress(object sender, KeyPressEventArgs e)
        {
            if(e.KeyChar=='\b'){
                //do nothing
            }
            else if ((through.Text.Trim().Length==4) || !Char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }

        }

        private void releaseYear_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\b')
            {
                //do nothing
            }
            else if ((releaseYear.Text.Trim().Length == 4) || !Char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }

        }

        private void ratingsHigh_KeyPress(object sender, KeyPressEventArgs e)
        {

            if (e.KeyChar == '\b')
            {
                //do nothing
            }
            else if ((ratingsHigh.Text.Trim().Length == 4))
            {
                e.Handled = true;
            }
            else if (e.KeyChar == '.')
            {
                if (ratingsHigh.Text.IndexOf('.') > -1)
                {
                    e.Handled = true;
                }
            }
            else if (!Char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }

        }

        private void ratingsLow_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\b')
            {
                //do nothing
            }
            else if ((ratingsHigh.Text.Trim().Length == 4))
            {
                e.Handled = true;
            }
            else if (e.KeyChar == '.')
            {
                if (ratingsHigh.Text.IndexOf('.') > -1)
                {
                    e.Handled = true;
                }
            }
            else if (!Char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void FormApplyFilter_Load(object sender, EventArgs e)
        {
            if (seenStatus.Text == "")
            {
                seenStatus.SelectedIndex = 0;
            }
        }


        

    }
}
